<div class="content-wrapper">
    <section class="content-header">
        <h1>{$enquiryInfo?'编辑':'新增'}询价</h1>
        <ol class="breadcrumb">
            <li><a href="/"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li class="active">项目管理</li>
            <li class="active">{$enquiryInfo?'编辑':'新增'}询价</li>
        </ol>
    </section>
    <section class="content">
        <form class="form-horizontal" role="form" action="javascrip:;" method="post">
            <div class="row">
                <div class="box">
                    <div class="box-header with-border">
                    </div>
                    <div class="box-body">
                        <div class="col-md-6">
                            <input type="hidden" name="id" id="id" value="{$enquiryInfo.id}" />
                            <div class="form-group">
                                <label for="form_name" class="col-sm-3 control-label"> 表单名称 </label>
                                <div class="col-sm-6">
                                    <input class="form-control" id="form_name" name="form_name" value="{$enquiryInfo.name}"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="form_status" class="col-sm-3 control-label"> 状态 </label>
                                <div class="col-sm-6">
                                    <select class="form-control" id="form_status" name="form_status">
                                        <option value="1" <if condition="$enquiryInfo.status eq 1 ">selected</if>>正常</option>
                                        <option value="-1" <if condition="$enquiryInfo.status eq -1 ">selected</if>>关闭</option>
                                    </select>
                                </div>
                            </div>
                            <!--添加表单元素-->
                            <div class="form-group">
                                <label for="parent_cid" class="col-sm-3 control-label"> 名称 </label>
                                <div class="col-sm-6">
                                    <input name="tpl_title" id="tpl_title" class="form-control"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="tpl_type" class="col-sm-3 control-label"> 类型 </label>
                                <div class="col-sm-6">
                                    <select class="form-control" id="tpl_type" name="tpl_type">
                                        <option value="0">请选择</option>
                                        <option value="input">文本</option>
                                        <option value="select">单选</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="tpl_value" class="col-sm-3 control-label"> 备选值 </label>
                                <div class="col-sm-6">
                                    <input type="text" class="form-control" id="tpl_value" name="tpl_value"/>
                                </div>
                                <label class="text text-danger" style="font-size: 12px">多值以英文逗号隔开</label>
                            </div>
                            <div class="form-group">
                                <label for="form-field-1" class="col-sm-3 control-label"></label>
                                <div class="col-sm-6">
                                    <button type="button" class="btn btn-info btn-sm add-btn">添加</button>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6" id="enquiry_tpl_detail">
                            <foreach name="enquiryInfo.enquiryDetail" item="item">
                                <if condition="$item.type eq 'input'">
                                    <div class="form-group">
                                        <input type="hidden" class="a_data" data-id="{$item.id}" data-title="{$item.title}" data-type="{$item.type}" data-values="{$item.data}"/>
                                        <label class="col-sm-3 control-label">{$item.title}</label>
                                        <div class="col-sm-6 input-group input-group-sm">
                                            <input class="form-control"/>
                                        </div>
                                    </div>
                                <else />
                                    <div class="form-group">
                                        <input type="hidden" class="a_data" data-id="{$item.id}" data-title="{$item.title}" data-type="{$item.type}" data-values="{$item.data}"/>
                                        <label class="col-sm-3 control-label">{$item.title}</label>
                                        <div class="col-sm-6 input-group">
                                            <php> $arr = explode(',',$item['data']);</php>
                                            <select class="form-control">
                                                <foreach name="arr" item="val">
                                                    <option value="{$val}">{$val}</option>
                                                </foreach>
                                            </select>
                                        </div>
                                    </div>
                                </if>
                            </foreach>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="box">
                        <div class="box-header with-border">
                            选择产品
                        </div>
                        <div class="box-body">
                            <div class="form-group">
                                <label for="select_items" class="control-label"></label>
                                <div class="col-sm-12">
                                    <input id="select_items" type="hidden" name="select_items" value="{$enquiryInfo.item_ids}"/>
                                    <div id="select_items_copy">
                                        <if condition="count($enquiryItem) eq 0 ">暂无商品信息</if>
                                        <foreach name="enquiryItem" item="item">
                                            <label class="col-sm-2">
                                                <input id="input-{$item.id}" type="checkbox" name="select_item" class="minimal-red" value="{$item.id}" <php>if(in_array($item['id'], explode(',',$enquiryInfo["item_ids"]))):echo checked; endif;</php>>
                                                {$item.parent_name}{$item.second_name}{$item.name}
                                            </label>
                                        </foreach>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="form-field-1" class="col-sm-3 control-label"></label>
                                <div class="col-sm-6">
                                    <button type="button" class="btn btn-info btn-md save-btn">提交</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </section>
</div>
<script type="application/javascript">
    function deleteTpl(obj) {
        $(obj).parent().parent().parent().remove();
    };
    $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
        checkboxClass: 'icheckbox_minimal-red',
        radioClass: 'iradio_minimal-red'
    });
    $(function () {
        //产品勾选
        $('input[name="select_item"]').on('ifChecked',function (event) {
            var id = $("#id").val();
            var strObj = $("#select_items").val();
            var strV = event.currentTarget.defaultValue;
            var target_id = event.currentTarget.id;
            $.post('/admin/enquiry/ajaxExistItem',
                {
                    id:id,
                    item_id:strV
                },function (data) {
                    if(data.Code!=999){
                        $('#'+target_id).iCheck('uncheck');
                        ArtDialogHlp.showInfo('提示',data.Msg);
                        return false;
                    }else{
                        if(!strObj){
                            strObj = new Array();
                        }else {
                            strObj = strObj.split(',')
                        }
                        strObj.push(strV);
                        strObj.join(',');
                        $("#select_items").val(strObj);
                    }
                },"JSON");

        }).on('ifUnchecked',function (event) {
            var strObj = $("#select_items").val();
            var strV = event.currentTarget.defaultValue;
            if(!strObj){
                strObj = new Array();
            }else {
                strObj = strObj.split(',')
            }
            var index = $.inArray(strV, strObj);
            if(index>-1){
                strObj.splice($.inArray(strV, strObj), 1);
            }
            strObj.join(',');
            $("#select_items").val(strObj);
        });
       //添加表单元素
       $('.add-btn').click(function () {
           var tpl_title = $("#tpl_title").val();
           var tpl_type = $("#tpl_type").val();
           var tpl_value = $("#tpl_value").val();
           if(tpl_title==''){
               ArtDialogHlp.showInfo('提示',"请填写表单元素名称");
               return false;
           }
           if(tpl_type==0){
               ArtDialogHlp.showInfo('提示',"请选择表单元素类型");
               return false;
           }
           if(tpl_type!='input' && tpl_value==''){
               ArtDialogHlp.showInfo('提示',"请填写表单元素备选值");
               return false;
           }
           if(tpl_type=='input'){
               var str = '';
               str+='<div class="form-group">';
               str+='<input type="hidden" class="a_data" data-id="" data-title="' + tpl_title + '" data-type="' + tpl_type + '" data-values="' + tpl_value + '"/>';
               str+='<label class="col-sm-3 control-label">' +tpl_title+ '</label>';
               str+='<div class="col-sm-6 input-group input-group-sm">';
               str+='<input class="form-control"/>';
               str+='<span class="input-group-btn">' +
                   '<button type="button" class="btn btn-danger btn-flat btn-delete-this" onclick="deleteTpl(this)">删</button>' +
                   '</span>';
               str+='</div>';
               str+='</div>';
           }else if(tpl_type=='select'){
               var arrObj = tpl_value.split(',');
               var str = '<div class="form-group">';
               str+='<input type="hidden" class="a_data" data-id="" data-title="' + tpl_title + '" data-type="' + tpl_type + '" data-values="' + tpl_value + '"/>';
               str+='<label class="col-sm-3 control-label">' +tpl_title+ '</label>';
               str+='<div class="col-sm-6 input-group">';
               str+='<select class="form-control">';
               arrObj.forEach(function (item,index) {
                   str+='<option value="' + item + '">' + item + '</option>';
               });
               str+='</select>';
               str+='<span class="input-group-btn">' +
                   '<button type="button" class="btn btn-danger btn-flat btn-delete-this" onclick="deleteTpl(this)">删</button>' +
                   '</span>';
               str+='</div>';
               str+='</div>';
           }
           $("#enquiry_tpl_detail").append(str);
       });
       $(".save-btn").click(function () {
           $('.save-btn').addClass('disabled'); // Disables visually
           $('.save-btn').prop('disabled', true); // Disables visually + functionally
           var detail = [];
           var id = $("#id").val();
           var item_ids = $("#select_items").val();
           var form_name = $("#form_name").val();
           var form_status = $("#form_status").val();
            $("#enquiry_tpl_detail .form-group").each(function(index,obj) {
                var input_data = $(obj).find('input[class="a_data"]');
                var id = input_data.attr('data-id');
                var title = input_data.attr('data-title');
                var type = input_data.attr('data-type');
                var values = input_data.attr('data-values');
                detail.push({id:id,title:title,type:type,values:values})
            });

           if(form_name==''){
               ArtDialogHlp.showInfo('提示','请填写表单名称');
               $('.save-btn').removeClass('disabled'); // Disables visually
               $('.save-btn').prop('disabled', false); // Disables visually + functionally
               return false;
           }
           if(detail.length<1){
               ArtDialogHlp.showInfo('提示','请添加表单元素');
               $('.save-btn').removeClass('disabled'); // Disables visually
               $('.save-btn').prop('disabled', false); // Disables visually + functionally
               return false;
           }
           if(item_ids==''){
               ArtDialogHlp.showInfo('提示','请选择产品');
               $('.save-btn').removeClass('disabled'); // Disables visually
               $('.save-btn').prop('disabled', false); // Disables visually + functionally
               return false;
           }
           $.post('/admin/enquiry/ajaxSaveEnquiry',{
               id: id,
               name: form_name,
               status: form_status,
               item_ids: item_ids,
               detail: detail
           },function (data) {
               $('.save-btn').removeClass('disabled'); // Disables visually
               $('.save-btn').prop('disabled', false); // Disables visually + functionally
               if(data.Code==999){
                   ArtDialogHlp.showInfo('提示',data.Msg,function () {
                       window.location ='/admin/enquiry/enquiryList';
                   });
               }else {
                   ArtDialogHlp.showInfo('提示',data.Msg);
               }
           },"JSON");
       });
    });
</script> 